<?php

namespace app\service;

use app\model\OmsOrderModel;
use app\model\DeliveryOrderModel;
use app\model\TaskModel;
use app\model\WarehouseModel;
use app\model\NetworksModel;
use app\model\XlShopGoodsModel;
use app\model\XlPurchaseOrderOutModel;
use app\model\XlPurchaseOrderGoodsOutModel;
use think\facade\Db;

class DeliveryOrderService
{

    public static function lists($param)
    {

      if (isset($param['task_id']) && !empty($param['task_id'])) {
          $info = TaskModel::info($param['task_id']);
          if (empty($info)) {
            return [];
          }
          $param['network_ids'] = $info['network_ids'];
      }
      $lists = DeliveryOrderModel::lists($param);
      foreach ($lists['lists'] as $key => $value) {
        $lists['lists'][$key]['ship_company'] = '';
        $lists['lists'][$key]['ship_people'] = '';
        $lists['lists'][$key]['ship_mobile'] = '';          
        $lists['lists'][$key]['receive_company'] = '';
        $lists['lists'][$key]['receive_people'] = '';
        $lists['lists'][$key]['receive_mobile'] = '';  
        $lists['lists'][$key]['delivery_company'] = '';
        $lists['lists'][$key]['belongs_company'] = '';
        
        $warehouseInfo = WarehouseModel::info($value['warehouse_id']);
        if ($warehouseInfo) {
          $lists['lists'][$key]['ship_company'] = $warehouseInfo['enterprise_name'];
          $lists['lists'][$key]['ship_people'] = $warehouseInfo['person_in_charge'];
          $lists['lists'][$key]['ship_mobile'] = $warehouseInfo['mobile'];
        }

        $networksInfo = NetworksModel::info($value['network_id']);
        if ($networksInfo) {
          $lists['lists'][$key]['receive_company'] = Db::table('xl_company')->where('id','=',$networksInfo['company_id'])->value('name');
          $lists['lists'][$key]['receive_people'] = $networksInfo['charge_person'];
          $lists['lists'][$key]['receive_mobile'] = $networksInfo['mobile']; 
        }

        if ($value['tms_task_id']) {
          $line_id = Db::table('xl_normal_task')->where('id','=',$value['tms_task_id'])->value('line_id');
          $lists['lists'][$key]['delivery_company'] = Db::table('xl_parnter_line')->where('id','=',$line_id)->value('transport_name');
        }

        if ($value['uid']) {
          $uid_arr = explode('-', $value['uid']);
          if (count($uid_arr) >= 3) {
            $lists['lists'][$key]['belongs_company'] = Db::table('xl_company')->where('id','=',$uid_arr[2])->value('name');
          }
        }
      }

      return $lists;
    }

    public static function oms_order_lists($param)
    {

        $info = DeliveryOrderModel::info($param['delivery_id']);
        if (empty($info)) {
          return [];
        }

        if (empty($info['oms_order_ids'])) {
          return [];
        }

        $param['order_ids'] = $info['oms_order_ids'];

        $lists = OmsOrderModel::lists($param);
        foreach ($lists['lists'] as $key => $value) {
          $lists['lists'][$key]['create_time'] = date('Y-m-d H:i:s', $value['create_time']);

          $orderOutInfo = Db::table('xl_purchase_orderout')->where('no', '=', $value['orderout_no'])->find();
          $lists['lists'][$key]['cate_count'] = $orderOutInfo['cate_count'];
          $lists['lists'][$key]['goods_count'] = $orderOutInfo['goods_count'];
        }
        return $lists;

    }

}
